package org.springService;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

/**
 * 消费者
 * @author 
 * @date   2018年9月13日
 */
public class ConsumerDemo {

	 public static void main(String[] args){
	        Properties properties = new Properties();
	        properties.put("bootstrap.servers", "localhost:9092,localhost1:9092");
	        properties.put("group.id", "group-1");
	        properties.put("enable.auto.commit", "true");
	        properties.put("auto.commit.interval.ms", "1000");
	        properties.put("auto.offset.reset", "earliest");
	        properties.put("session.timeout.ms", "30000");
	        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
	        properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
	        @SuppressWarnings("resource")
			KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties);
	        //kafkaConsumer.subscribe(Arrays.asList(ProducerDemo.TOPIC1,ProducerDemo.TOPIC2));
	        kafkaConsumer.subscribe(Arrays.asList(ProducerDemo.TOPIC));
	        while (true) {
	        	try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	            ConsumerRecords<String, String> records = kafkaConsumer.poll(100);
	            for (ConsumerRecord<String, String> record : records) {
	                System.out.printf("接受卡夫卡消息内容为——————————————》》offset = %d, value = %s", record.offset(), record.value());
	                System.out.println();
	            }
	        }

	    }
}
